<?php
namespace Admin\Controller;

class CategoryController extends AdminController
{
	//分类首页
	public function index ()
	{
		/*if(I('get.pid/d')) {
			$id = I('get.pid/d');
			$count = M('category')->where('id='.$id)->count();
		} else {*/
		if(I('get.id/d')) {
			$pid = I('get.id/d');
		} else {
			$pid = 0;
		}
			$count = M('category')->where('pid='.$pid)->count();
		/*}*/
		$page = new \Think\Page($count,8);
		$show = $page->show();
		if (!empty($_POST)) {
			/*var_dump($_POST); exit;*/
			$list = $_POST['name'];
			$data['name'] = array('like','%'.$list.'%'); 
			//var_dump($data);
			
			/*var_dump($data);exit;*/
			/*if(I('get.pid/d')) {
				$id = I('get.pid/d');
				$p = $data.'and id='.$id;
			} else {
				$p = $data.'and pid='.$pid;
			}*/
			$list = M('category')->where($data.'and pid='.$pid)->limit($page->firstRow.','.$page->listRows)->select();
		} else {
			/*if(I('get.pid/d')) {
				$id = I('get.pid/d');
				$p = 'id='.$id;
			} else {
				$p = 'pid='.$pid;
			}*/
			$list = M('category')->where('pid='.$pid)->limit($page->firstRow.','.$page->listRows)->order('id asc')->select();
			//var_dump($list);exit;
		}
		$p1 = M('category')->find($pid);
		$p2 = $p1['pid'];
		if(!$list) {
			//var_dump($list);
			$this->assign ('row','暂无搜索内容...');
		} 
		//var_dump($list);exit;
		/*$page->setConfig('next','下一页');
		$this->assign('data',$data);*/
		$this->assign('p2',$p2);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	//分类添加页
	public function add ()
	{
		if (!empty (I ('get.id/d'))) {
			$data = M ('category')->find (I ('get.id/d'));
			//var_dump($data);exit;
			$this->assign('list',$data);
			$this->display();
		} else {
			$this->display();
		}
	}
	public function insert ()
	{
		//var_dump($_POST);exit;
		$category = D ('category');
		if(!$category->create ()) {
			$this->error ($category->getError());
		} else {
			//var_dump($da = $category->fetchSql(ture)->add ());exit;
			if ($category->add () > 0) {
				$this->success ('添加成功',U('Category/index'));
			} else {
				$this->error ('添加失败');
			}
		}
	}
	//分类编辑页
	public function edit ()
	{
		$id = I('get.id/d');
		$list = M('category')->find($id);
		//var_dump($list);exit;
		$this->assign('list',$list);
		$this->display ();
	}
	public function update ()
	{
		//var_dump($_POST);exit;
		$category = D('category');
		//var_dump($category);exit;
		if (!$category->create ()) {
			$this->error ($category->getError());

		} else {
			//var_dump($category->create ());exit;
			if($category->save ()!==false) {
				$this->success ('修改成功',U('Category/index'));
			} else {
				$this->error ('修改失败');
			}
		}
	}
	public function update1 ()
	{
		//var_dump($_GET);exit;
		$id = I('get.id/d');
		$display = M('category')->where('id='.$id)->find();
		$display = $display['display'];
		//var_dump($display);exit;
		switch ($display) {
			case '1':
			$data['id'] = $id;
			$data['display'] = 0;
			$category = D ('category');
		if (!$category->create ($data)) {
			$this->error ($category->getError());
		} else {
			if ($category->save()!==false) {
				
					echo '显示';
				}
			
		}
			break;
			case '0':
			$data['id'] = $id;
			$data['display'] = 1;
			$category = D ('category');
		if (!$category->create ($data)) {
			$this->error ($category->getError());
		} else {
			if ($category->save()!==false) {
				
					echo '隐藏';
				}
			
		}
			break;

		}
		/*$category = D ('category');
		if (!$category->create ($_GET)) {
			$this->error ($category->getError());
		} else {
			if ($category->save()!==false) {
				if($_GET['display']==1) {
					echo '隐藏';
				} else {
					echo '显示';
				}
			}
		}*/
		
	}
	//删除
	public function delete ()
	{
		$id = I ('get.id/d');
		$data = M('category')->where('pid='.$id)->select();
		if(!empty($data)){
			$this->error ('请先删除子分类');
			exit;
		}
		if (M ('category')->delete ($id)) {
			$this->success ('删除成功',U ('Category/index'));
		} else {
			$this->error ('删除失败');
		}
	}
}